// ActionScript file
package com.wdxc {
    import flash.display.MovieClip;
    import flash.display.Sprite
    import flash.display.* ;
    import flash.events.MouseEvent ;  
    import flash.geom.Matrix ;
    /**
    * 图片旋转
    * @author luqinglong
    */ 
    public class ImageRotation extends Sprite{
         
          
        private var _bitmap:Bitmap  ;
          
        private var _x:Number ;
        private var _y:Number ;
        private var _w:Number ;
        private var _h:Number ; 
           
        //private var _angle:Number ;
        public function ImageRotation() { 
              
            _bitmap = Main.SCENE.bitmap  ;
            _x = _bitmap.x ;
            _y = _bitmap.y ;   
            _w = _bitmap.width  ;
            _h = _bitmap.height ;
            cancle_btn.addEventListener(MouseEvent.CLICK, cancleHandler);
            
            left_btn.addEventListener(MouseEvent.CLICK, leftRotationHandler);    
            right_btn.addEventListener(MouseEvent.CLICK, rightRotationHandler) ;
            save_btn.addEventListener(MouseEvent.CLICK, applicationHandler);
              
            //单击了旋转
            Main._handerState = 10 ;
            //go_cut_btn.addEventListener(MouseEvent.CLICK, goCutMenu);
        }
              
        /*
        private function goCutMenu(e:MouseEvent):void {
            this.remove();     
            Main.SCENE._imageCut.cutpicing();
        }*/
        
        /**
         * 取消操作
         * @param   e
         */
        private function cancleHandler(e:MouseEvent):void {
            //Main.SCENE.reset() ; 
            Main._handerState = 0 ;
            this.remove();  
        }   
              
        public function remove():void {
            Main.SCENE.bitmap.x = _x ;   
            Main.SCENE.bitmap.y = _y ;            
            Main.SCENE.bitmap.rotation = 0 ;
            left_btn.removeEventListener(MouseEvent.CLICK, leftRotationHandler);
            cancle_btn.removeEventListener(MouseEvent.CLICK, cancleHandler) ;
            save_btn.removeEventListener(MouseEvent.CLICK, applicationHandler);
            //go_cut_btn.removeEventListener(MouseEvent.CLICK, goCutMenu);
                 
            this.stage.removeChild(this) ;  
        }
        
        private function applicationHandler(e:MouseEvent):void { 
  
            Main.SCENE.addVagueBg();    
            Main.SCENE.addLoadingBar(); 
               
            var server :ImageService = new ImageService();
            server.photoid = Main.SCENE.photoid ;      
            server.submitRotation(fixAng( _bitmap.rotation) ); 
    
            left_btn.removeEventListener(MouseEvent.CLICK, leftRotationHandler);
            cancle_btn.removeEventListener(MouseEvent.CLICK, cancleHandler) ;
            save_btn.addEventListener(MouseEvent.CLICK, applicationHandler);
            this.stage.removeChild(this) ;
              
            Main._handerState = 2  ;
            
        }
      
        /**
         * 左旋转处理
         * @param   e
         */      
        private function rightRotationHandler(e:MouseEvent):void {
            var _angle = fixAng(_bitmap.rotation) ;
             
            if (_angle== 0) {    
                _bitmap.x = _x + (_h + _w) / 2 ;
                _bitmap.y = _y+(_h - _w) / 2 ;          
                 
            }                
            else if (_angle == 90) {
                
                _bitmap.x = _x + _w ;    
                _bitmap.y = _y + _h ; 
            }     
            else if (_angle== 180) {
                _bitmap.x = _x - (_h - _w) / 2;               
                _bitmap.y = _y + (_w+_h)/2 ;
              
            }             
            else if (_angle == 270) {   
                _bitmap.x = _x ;    
                _bitmap.y = _y;    
                      
            }
                    
            _bitmap.rotation += 90 ;   
            _angle = fixAng( _bitmap.rotation) ;
            trace("angle="+_angle);
        }
        private function leftRotationHandler(e:MouseEvent):void {
     
            var _angle = fixAng( _bitmap.rotation) ;
            if (_angle == 0) {        
                _bitmap.x = _x - (_h - _w) / 2;               
                _bitmap.y = _y + (_w+_h)/2 ;    
            }                 
            else if (_angle == 270) {  
                _bitmap.x = _x + _w ;        
                _bitmap.y = _y + _h ;     
            }                 
            else if (_angle == 180 ){ 
                _bitmap.x = _x + (_h + _w) / 2 ;
                _bitmap.y = _y+(_h - _w) / 2 ;     
                                         
            }    
            else if (_angle == 90) {   
                _bitmap.x = _x ;    
                _bitmap.y = _y;    
            }            
                   
            _bitmap.rotation -= 90 ; 
            trace("angle="+_angle);
        }
         
        /**  
         * 转换成标准角度
         * @param   _ang
         */
        public static function fixAng (_ang:Number){
            var _ang:Number = _ang % 360;
            var ang:Number = _ang % 360 < 0 ? _ang + 360 : _ang;
            return ang;
        }
        
    }
    
}